﻿#region Using Statements
using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Collections;
using RhinoXNA.RhinoGraphics.Rhino2D;
#endregion

namespace RhinoXNA.RhinoUtilities
{
    public class RhinoLoader
    {
        static Hashtable _types;

        public static List<IRhinoGraphicsComponent> GetGraphics(RhinoXNAAdapter adapter, String propertiesFile, String fileToLoad)
        {
            _types = LoadProperties(propertiesFile);
            return LoadGraphics(adapter, _types, fileToLoad);
        }

        private static List<IRhinoGraphicsComponent> LoadGraphics(RhinoXNAAdapter adapter, Hashtable types, String fileToLoad)
        {
            List<IRhinoGraphicsComponent> graphics = new List<IRhinoGraphicsComponent>();

            try
            {
                using (StreamReader sr = new StreamReader(StorageContainer.TitleLocation + "\\Content\\" + fileToLoad))
                {
                    String line;

                    while ((line = sr.ReadLine()) != null)
                    {
                        line = line.Replace(" ", "");
                        String[] temp = line.Split(',');

                        if (temp[0].StartsWith("'") != true && line.Replace(" ", "") != "")
                        {
                            String objTypeString = types[temp[0]].ToString();
                            System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(Directory.GetCurrentDirectory()
                                + "\\"
                                + System.Reflection.Assembly.GetEntryAssembly().FullName.Substring(0, System.Reflection.Assembly.GetEntryAssembly().FullName.IndexOf(","))
                                + ".exe");
                            Type classType = assembly.GetType(objTypeString);

                            if (classType != null)
                            {
                                List<String> parameters = new List<string>(temp);
                                object obj = Activator.CreateInstance(classType, new object[] { adapter, parameters });
                                graphics.Add(obj as IRhinoGraphicsComponent);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                //QUIT!
                throw new Exception("Error Parsing Load File - Shutting Down");
            }

            return graphics;
        }

        public static List<IMenuItem> GetMenuItems(RhinoXNAAdapter adapter, String propertiesFile, String fileToLoad)
        {
            _types = LoadProperties(propertiesFile);
            return LoadMenuItems(adapter, _types, fileToLoad);
        }

        private static List<IMenuItem> LoadMenuItems(RhinoXNAAdapter adapter, Hashtable types, String fileToLoad)
        {
            List<IMenuItem> graphics = new List<IMenuItem>();

            try
            {
                using (StreamReader sr = new StreamReader(StorageContainer.TitleLocation + "\\Content\\" + fileToLoad))
                {
                    String line;

                    while ((line = sr.ReadLine()) != null)
                    {
                        line = line.Replace(" ", "");
                        String[] temp = line.Split(',');

                        if (temp[0].StartsWith("'") != true && line.Replace(" ", "") != "")
                        {
                            String objTypeString = types[temp[0]].ToString();
                            System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(Directory.GetCurrentDirectory()
                                + "\\"
                                + System.Reflection.Assembly.GetEntryAssembly().FullName.Substring(0, System.Reflection.Assembly.GetEntryAssembly().FullName.IndexOf(","))
                                + ".exe");
                            Type classType = assembly.GetType(objTypeString);

                            if (classType != null)
                            {
                                List<String> parameters = new List<string>(temp);
                                object obj = Activator.CreateInstance(classType, new object[] { adapter, parameters });
                                graphics.Add(obj as IMenuItem);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                //QUIT!
                throw new Exception("Error Parsing Load File - Shutting Down");
            }

            return graphics;
        }

        private static Hashtable LoadProperties(String propertiesFile)
        {
            Hashtable properties = new Hashtable();

            try
            {
                using (StreamReader sr = new StreamReader(StorageContainer.TitleLocation + "\\Content\\" + propertiesFile))
                {
                    String line;

                    while ((line = sr.ReadLine()) != null)
                    {
                        line = line.Replace(" ", "");
                        String[] temp = line.Split('=');

                        if (temp[0].StartsWith("'") != true && line.Replace(" ", "") != "")
                        {
                            properties.Add(temp[0], temp[1]);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                //QUIT!
                throw new Exception("Error Parsing Properties File - Shutting Down");
            }

            return properties;
        }

        public static Vector2 GetPosition(RhinoXNAAdapter adapter, Texture2D texture, String xPos, String yPos)
        {
            Vector2 pos = new Vector2();
            if (xPos.ToLower() == "center")
                pos.X = adapter.SCREEN_SAFE.Center.X;
            else if (xPos.StartsWith("C"))
                pos.X = adapter.SCREEN_SAFE.Center.X + float.Parse(xPos.Substring(1));
            else if (xPos.StartsWith("L"))
                pos.X = adapter.SCREEN_SAFE.Left + float.Parse(xPos.Substring(1));
            else if (xPos.StartsWith("R"))
                pos.X = adapter.SCREEN_SAFE.Right - float.Parse(xPos.Substring(1));
            else
                pos.X = float.Parse(xPos);

            if (yPos.ToLower() == "center")
                pos.Y = adapter.SCREEN_SAFE.Center.Y;
            else if (yPos.StartsWith("C"))
                pos.Y = adapter.SCREEN_SAFE.Center.Y + float.Parse(yPos.Substring(1));
            else if (yPos.StartsWith("B"))
                pos.Y = adapter.SCREEN_SAFE.Bottom - float.Parse(yPos.Substring(1));
            else if (yPos.StartsWith("T"))
            {
                if (yPos.StartsWith("TBG"))
                    pos.Y = texture.Height / 2 + float.Parse(yPos.Substring(3));
                else
                    pos.Y = adapter.SCREEN_SAFE.Top + float.Parse(yPos.Substring(1));
            }
            else
            {
                pos.Y = float.Parse(yPos);
            }

            return pos;
        }

        public static Vector2 GetPosition(RhinoXNAAdapter adapter, String xPos, String yPos)
        {
            Vector2 pos = new Vector2();

            if (xPos.ToLower() == "center")
                pos.X = adapter.SCREEN_SAFE.Center.X;
            else if (xPos.StartsWith("C"))
                pos.X = adapter.SCREEN_SAFE.Center.X + float.Parse(xPos.Substring(1));
            else if (xPos.StartsWith("L"))
                pos.X = adapter.SCREEN_SAFE.Left + float.Parse(xPos.Substring(1));
            else if (xPos.StartsWith("R"))
                pos.X = adapter.SCREEN_SAFE.Right - float.Parse(xPos.Substring(1));
            else
                pos.X = float.Parse(xPos);

            if (yPos.ToLower() == "center")
                pos.Y = adapter.SCREEN_SAFE.Center.Y;
            else if (yPos.StartsWith("C"))
                pos.Y = adapter.SCREEN_SAFE.Center.Y + float.Parse(yPos.Substring(1));
            else if (yPos.StartsWith("B"))
                pos.Y = adapter.SCREEN_SAFE.Bottom - float.Parse(yPos.Substring(1));
            else if (yPos.StartsWith("T"))
                pos.Y = adapter.SCREEN_SAFE.Top + float.Parse(yPos.Substring(1));
            else
                pos.Y = float.Parse(yPos);

            return pos;
        }

        public static Vector2 GetBoundingVolume(Texture2D texture, String width, String height)
        {
            Vector2 boundingSize = new Vector2(0.0f, 0.0f);

            if (width.ToLower() == "width")
                boundingSize.X = texture.Width;
            else
                boundingSize.X = float.Parse(width);

            if (height.ToLower() == "height")
                boundingSize.Y = texture.Height;
            else
                boundingSize.Y = float.Parse(height);

            return boundingSize;
        }
    }
}